package cn.sinjet.database;

import java.util.ArrayList;

import com.mgroup.wazehelper.R;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import cn.sinjet.carassist.SinjetApplication;
import cn.sinjet.entity.MyObdItemData;

public class OBDDAO {
	private SQLiteDatabase database = null;
    private final int VERSION = 6;
    private final String DB_DRIVER_ASSISTER = "da.wav";
    
    
     private  SQLiteDatabase getDatabase() {
    	 if(database != null)
    		 return database;
    	 
       	 if(database == null){
       		Context context =  SinjetApplication.getInstance().getApplicationContext();
     		SQLlightHelper dbManager = new SQLlightHelper(context);
     		if (database != null) {
     			database.close();
     		}
     		database = dbManager.openDatabase(DB_DRIVER_ASSISTER, R.raw.da);
     		if (database.getVersion() != VERSION) {
     			dbManager.delete(DB_DRIVER_ASSISTER);
     			database = dbManager.openDatabase(DB_DRIVER_ASSISTER, R.raw.da);
     			database.setVersion(VERSION);
     		} 
       	 }
 		 return database;
 	 }


 	public  void close() {
 		if (database != null) {
 			database.close();
 			database = null;
 		}
 	}

 	
 	public  MyObdItemData getObdItemData(String code) {
 		try{
	 		SQLiteDatabase db = getDatabase();
	 		if(db == null)
	 			return null;
	 		
			MyObdItemData item = new MyObdItemData();
			String strTableName = "DICT_OBD";
			Cursor cur = db.rawQuery("select * from " + strTableName
					+ " where code = " + "'" + code + "'", null);
	
			if (cur != null) {
				if (cur.getCount() > 0) {
					cur.moveToFirst();
	
					item.m_id = cur.getInt(cur.getColumnIndex("id"));
					item.m_strCode = cur.getString(cur.getColumnIndex("code"));
					item.m_strChineseName = cur.getString(cur
							.getColumnIndex("chineseName"));
					item.m_strEnglishName = cur.getString(cur
							.getColumnIndex("englishName"));
					item.m_strCategory = cur.getString(cur
							.getColumnIndex("category"));
					item.m_strDiscription = cur.getString(cur
							.getColumnIndex("discription"));
					item.m_strType = cur.getString(cur.getColumnIndex("type"));
					item.m_strCarName = cur.getString(cur.getColumnIndex("car"));
	
					System.out.println("m_id = " + item.m_id + " m_strCode = "
							+ item.m_strCode + " m_strChineseName = "
							+ item.m_strChineseName + " m_strEnglishName = "
							+ item.m_strEnglishName + " m_strCategory = "
							+ item.m_strCategory + " m_strDiscription = "
							+ item.m_strDiscription + " m_strType = "
							+ item.m_strType + " m_strCarName = "
							+ item.m_strCarName);
					return item;
				}
			}
 		}catch(SQLiteException e){
 			e.printStackTrace();
 		}finally{
 			
 		}

		return null;
	}

	public  ArrayList<String> getObdItemLike(String like) {
		try{
			SQLiteDatabase db = getDatabase();
	 		if(db == null)
	 			return null;
			ArrayList<String> list = new ArrayList<String>();	
			String strTableName = "DICT_OBD";
			String strLimit = String.format("%d", (like.length() == 5) ? 1 : 200);
			Cursor cur = db.rawQuery("select * from " + strTableName
					+ " where code like " + "'%" + like
					+ "%' order by code LIMIT " + strLimit, null);
	
			if (cur != null) {
				int n = cur.getCount();
				if (n > 0) {
					cur.moveToFirst();
					while (n > 0) {
	
						String t = cur.getString(cur.getColumnIndex("code"));
						list.add(t);
	
						String strLog = "like " + like + " " + t;
						logOut(strLog);
						n--;
	
						if (n > 0)
							cur.moveToNext();
	
					}
	
				}
			}
			return list;
		}catch(SQLiteException e){
			e.printStackTrace();
		}finally{
			
		}
		return null;
	}
	 	
	public static void logOut(String s) {
		System.out.println(s);
	}
}
